package com.cos.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseCode;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.entity.User;
import com.cos.shop.services.UserServices;
import com.cos.shop.services.impl.UserServicesImpl;
import com.cos.shop.util.Base64Utils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author: COS
 * @time: 2023/5/9 15:31
 * @description:
 */
@WebServlet("/user")
public class UserController extends BaseServlet {
    private UserServices us = new UserServicesImpl();
    /**
     * @author: COS
     * @time: 2023/5/10 15:51
     * @description: 处理登录的请求 (未完成)
     */
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        ResponseResult rr = us.doLogin(username,password);
        if (rr.getCode()== ResponseCode.SUCCESS.getCode()) {
            //保存数据到session中
            request.getSession().setAttribute(Const.LOGIN_INFO,rr.getT());
            // 新增 自动登录处理
            String auto = request.getParameter("auto");
            // 没有选中为null,选中为no默认值
            if (auto != null) {
                //创建cookie 保存用户名和密码
                String v = username+":"+password;
                // cookie 有安全隐患,所以把用户名和密码进行编码出库
                Cookie c= new Cookie(Const.AUTO_KEY, Base64Utils.encode(v));
                //设置cookie 存活时间 和作用范围
                c.setMaxAge(Const.Time.TWO_WEEK_SEC);
                c.setPath("/");
                response.addCookie(c);
            }
            //直接重定向到首页
            return Const.REDIRECT+"/index.jsp";
        }
        request.setAttribute("msg", rr);
        return Const.FORWARD+"login.jsp";
    }
    /**
     * @author: COS
     * @time: 2023/5/10 15:51
     * @description: 检查用户名是否存在的请求
     */
    public String checkName(HttpServletRequest request, HttpServletResponse response) {
        //1 接受用于提交的数据
        String username = request.getParameter("username");
        //2 访问业务层 {"name":"zs","age":18} []
        ResponseResult result = us.findUserByNameByNewResult(username);
        //3 根据业务结果给不同的响应  ajax  转换json
        String s = JSON.toJSONString(result);
        return s;
    }
    /**
     * @author: COS
     * @time: 2023/5/10 15:51
     * @description: 注册
     */
    public String reg(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //1 接受用户提交数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //2 创建要封装数据的对象
        User u = new User();
        //3 使用工具类封装
        BeanUtils.populate(u, parameterMap);
        //4 业务层处理新增业务
        ResponseResult rr = us.reg(u);
        //5 存响应数据到request作用域
        if (rr.getCode() == ResponseCode.SUCCESS.getCode()) {
            // 直接重定向到默认注册成功页面
            return Const.REDIRECT+"/registerSuccess.jsp";
        }
        request.setAttribute(Const.MSG, rr);
        return Const.MESSAGE;
    }





    /* 弃用 */
    // public String checkName_old(HttpServletRequest request, HttpServletResponse response){
    //     //1 接受用于提交的数据
    //     String username = request.getParameter("username");
    //     //2 访问业务层
    //     User user = us.findUserByName(username);
    //     //3 根据业务结果给不同的响应
    //     if (user != null) {
    //         return "1";
    //     }
    //     return "0";
    // }
}
